<?php
namespace SYSGL\Controller;               /* 命名空间。表示当前类是Home模块下控制器类 */
use Common\Controller\BaseController;     /* 表示引入Common\Controller\BaseController命名空间便于直接使用 */

class IndexController extends BaseController {  /* 等同于 “class IndexController extends Common\Controller\BaseController”，即子类“IndexController.class.php”继承父类“BaseController.class.php”  */
	
	public $classify_list_top = array();
	//Index 每一个方法都要调用到的
	protected function _initialize(){    /* 申明函数_initialize()函数 */
		parent::_initialize();           /* 调用类_initialize的方法 */
		$listEmpty = "<div class=\"newscase\">暂时没有数据</div>";
		$this->assign("listEmpty",$listEmpty);

		//TOP 导航栏
		$_top_menu = M('classify')->field('id,name,subname,link')->where('status=1 and id in(7,14) and pid=0')->order('sort desc')->limit(6)->select();
		$this->assign('top_menu', $_top_menu);
		$this->classify_list_top = $this->getSubByKey($this->classify_list,'id');

		//FOOT 导航栏
		$_foot_menu = M('classify')->field('id,name,subname,link')->where('status=1 and pid=0')->order('sort desc')->limit(11)->select();
		$this->assign('foot_menu', $_foot_menu);		
	}
	
	// 校园新闻
	private function _index_xyxw(){
		$_sql  = "select count(id) num,FROM_UNIXTIME(times,'%Y-%m-%d') rq,".
				"FROM_UNIXTIME(times,'%m') rq_m,FROM_UNIXTIME(times,'%d') rq_d ".
				"from app_news where status=1 group by FROM_UNIXTIME(times,'%Y-%m-%d') ".
				"order by times desc limit 5";
		$list = M()->query($_sql);
		foreach ($list as $_k => &$_v) {
			$_v['_list_forRQ'] = M('news')->field('id,pid,title,content,times,desc')
					->where("status=1 and FROM_UNIXTIME(times,'%Y-%m-%d') = '".$_v['rq']."'")
					->order('sort desc,id desc')->limit(3)->select();
		}
		$this->assign('IL_xyxw', $list);
		$this->assign("IL_xyxw_class",explode(",","na,nb,nc,nd,ne"));
	}
	/**
	 * Action WebSite首页
	 * @return [type] [description]
	 */
	public function index() {
		//实验验约
		$this->_index_xyxw();

		$info = array(
					'IL_syyy' => array('id' => '13', 'num' => 4),	//实验验约
				);
		$indexList = array();
		foreach($info as $k=>$v){
			$list = M('news')->field('id,pid,title,times,desc')->where('status=1 and pid in (' . $v['id'] . ')')
							->order('sort desc,id desc')->limit($v['num'])->select();
			$indexList[$k] = $list;
		}
		$this->assign('indexList', $indexList);

		//轮播
		$info = array(
					'lb1' => array('id' => '1', 'num' => 4),	//首页轮播图-banner
					'lb2' => array('id' => '2', 'num' => 2),	//专题网站外链
					'lb3' => array('id' => '3', 'num' => 5),	//首页图片外链
					'lb4' => array('id' => '5', 'num' => 6),	// 实验室s
				);
		$indexSlideList = array();
		foreach($info as $k=>$v){
			$list = M('slide')->field('id,cid,name,pic')->where('status=1 and cid in (' . $v['id'] . ')')
							->order('sort desc,id asc')->limit($v['num'])->select();
			$indexSlideList[$k] = $list;
		}
		$this->assign('indexSlideList', $indexSlideList);		
	    $this->display();
	}

	/**
	 * 获取一级导航ID，当前栏目信息
	 */
	private function _get_classify_info($id){
		//获取当前分类信息
		$classify_info = $this->getInfo('id,pid,name,subname,pic,link', 'classify', 'id=' . $id);
		$this->assign('classify_info', $classify_info);

		//得到当前的一级导航，ID
		$_top_pid = $classify_info['pid'] > 0 ? $classify_info['pid'] : $id;
		$this->assign('top_pid', $_top_pid);
		
		return array("_top_pid"=>$_top_pid,"classify_info"=>$classify_info);
	}

	/**
	 * 获取当前一级导航信息 & 相应二级导航的信息
	 */
	private function _get_classify_list_sub($_top_pid){
		//获取分类及子分类信息
		$classify_list_sub = $this->getList('id,pid,name,subname,link', 'classify', 'status=1 and (id= '.$_top_pid.' or pid=' . $_top_pid . ')', 'sort desc');
		$_top_pid_menuInfo = array();
		foreach ($classify_list_sub as $_k => &$_v) {
			if($_v['pid'] == 0){
				$_top_pid_menuInfo = $_v;
				unset($classify_list_sub[$_k]);
			}else{
				$_v['child_sub'] = $this->getList('id,pid,name,subname,link', 'classify', 'status=1 and (pid=' . $_v['id'] . ')', 'sort desc');
				$_v['child_sub_num'] = count($_v['child_sub']);
			}
		}
		//设置当前所在一级导航，菜单信息
		$this->assign('top_pid_menuInfo', $_top_pid_menuInfo);
		//二级菜单信息
		$this->assign('classify_list_sub', $classify_list_sub);
		return $classify_list_sub;
	}

	/**
	 * Action 新闻列表页
	 * @return [type] [description]
	 */
	public function lists(){
		$id = I('get.id', 0, 'intval');
		if($id > 0){
			//获取一级导航ID，当前栏目信息
			$_get_info = $this->_get_classify_info($id);
			$_top_pid = $_get_info['_top_pid'];				//得到当前的一级导航，ID
			$classify_info = $_get_info['classify_info'];	//获取当前分类信息

			if ($classify_info){
				//获取子菜单
				$classify_list_sub = $this->_get_classify_list_sub($_top_pid);
				//获取当前分类及子分类新闻
				if($classify_info['pid'] == 0){
					foreach ($classify_list_sub as $key => $value) {
						$ids[] = $value['id'];
					}
					if(!$ids){
						$ids[] = $id;
					}
				}else{
					$ids[] = $id;
				}
				$news_list = $this->getList('id,pid,title,pic,content,times,view,desc', 'news', 'status=1 and pid in (' . implode(',', $ids) . ')', 'sort desc,times desc', true,4);
				$this->assign('news_list', $news_list);
				$this->display();
			}
		}
	}

	/**
	 * Action 子站点
	 * @return [type] [description]
	 */
	public function subset(){
		$id = I('get.id', 0, 'intval');
		if($id > 0){
			//获取一级导航ID，当前栏目信息
			$_get_info = $this->_get_classify_info($id);
			$classify_info = $_get_info['classify_info'];	//获取当前分类信息

			if ($classify_info){
				//获取子菜单
				$classify_list_sub = $this->_get_classify_list_sub($id);
				$_list_num = 5;
				$indexList = array();
				if(count($classify_list_sub) > 1){
					foreach($classify_list_sub as $v){
						$list = M('news')->field('id,pid,title,times,desc')->where('status=1 and pid in (' . $v['id'] . ')')
							->order('sort desc,id desc')->limit($_list_num)->select();
						$indexList[] = array(
											"sub_info"=>$v,
											"sub_data"=>$list
										);
					}
					$this->assign('indexList', $indexList);
					$this->display("subset");
				}else{
					$this->assign('indexList', $indexList);
					$this->display("subsetOne");
				}
			}
		}
	}

	/**
	 * Action 具体新闻页
	 * @return [type] [description]
	 */
	public function news(){
		$id = I('get.id', 0, 'intval');
		if($id > 0){
			//获取当前新闻信息
			$news_info = $this->getInfo('id,pid,title,times,view,content,type', 'news', 'status=1 and id=' . $id);
			if($news_info){
				//更新浏览数
				M('news')->where(array('id' => $id))->setInc('view');
				if($news_info['type'] == 1){
					preg_match_all('/<img.*?src="(.*?)".*?alt="(.*?)".*?>/is', $news_info['content'], $news_info['img']);
				}
				$this->assign('news_info', $news_info);


				//获取一级导航ID，当前栏目信息
				$_get_info = $this->_get_classify_info($news_info['pid']);
				$_top_pid = $_get_info['_top_pid'];				//得到当前的一级导航，ID
				$classify_info = $_get_info['classify_info'];	//获取当前分类信息
				//获取当前分类及子分类
				$classify_list_sub = $this->_get_classify_list_sub($_top_pid);
				$this->display();
			}
		}
	}

	/**
	 * Action 站内搜索
	 * @return [type] [description]
	 */
	public function reserch(){
		//设置当前所在一级导航，菜单信息
		$_top_pid_menuInfo = array('pid' => 0, 'name' => '首页搜索', 'subname' => 'RESERCH');
		$this->assign('top_pid_menuInfo', $_top_pid_menuInfo);

		$this->assign('classify_info', array('pid' => 0, 'name' => '首页搜索', 'subname' => 'RESERCH'));

		$keyword = I('get.keyword', 0, 'strval');
		if($keyword){
			//获取当前分类新闻
			$news_list = $this->getList('id,title,times,view,desc', 'news',
						'status=1 and (title like "%' . $keyword . '%" or content like "%' . $keyword . '%")', 'sort desc,times desc', true);
		}
		$this->assign('news_list', $news_list);
		$this->display('reserch');
	}
}